﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZPod.MTG.Communication.Remoting;
using System.Configuration;
using System.Diagnostics;

namespace RemotingServer
{
	class Program
	{
		static void Main(string[] args)
		{
			try
			{
				string port = ConfigurationManager.AppSettings["Port"];
				string serverName = ConfigurationManager.AppSettings["ServerName"];
				string debug = ConfigurationManager.AppSettings["debug"];

				bool debugFlag = false;
				bool d;
				if (Boolean.TryParse(debug, out d))
				{
					debugFlag = d;
				}

				if (debugFlag)
				{
					Debugger.Launch();
				}
				

				int portNumber = 5678; //default port
				if (!String.IsNullOrEmpty(port))
				{
					int n;
					if (Int32.TryParse(port, out n))
					{
						portNumber = n;
					}
				}

				if (String.IsNullOrEmpty(serverName))
				{
					serverName = "GameServer"; //default name
				}

				Server.CreateGameServer(portNumber, serverName);

				Console.ForegroundColor = ConsoleColor.Cyan;
				Console.Write("Servidor iniciado en ");
				Console.ForegroundColor = ConsoleColor.Green;
				Console.WriteLine(String.Format("tcp://localhost:{0}/{1}",
					portNumber,
					serverName));
				Console.ResetColor();
				Console.WriteLine("Presione cualquier tecla para finalizar...");
				Console.ReadKey();
			}
			catch
			{
				Console.WriteLine("No se pudo iniciar el servidor. Verifique que el puerto esté disponible.");
			}
		}
	}
}
